package com.scilvet.reportcomposer.report;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class Report implements Iterable<Line> {

	private String name;
	private Map<Integer, Line> lineMap;
	
	public Report(String name) {
		this.name = name;
		this.lineMap = new LinkedHashMap<Integer, Line>();
	}
	
	public String getName() {
		return name;
	}
	
	protected void addLine(Line line) {
		
		if (line == null) {
			throw new IllegalArgumentException("Null line passed as parameter to addLine");
		}
		
		if (lineMap.containsKey(line.getLineNumber())) {
			throw new IllegalArgumentException("Duplicate entry for line number" + line.getLineNumber());
		}
		
		lineMap.put(line.getLineNumber(), line);
	}
	
	protected Line getLine(Integer lineNumber) {
		
		if (lineNumber == null) {
			throw new IllegalArgumentException("Null line passed as parameter to getLine");
		}
		
		return lineMap.get(lineNumber);
	}
	
	protected boolean containsLine(Integer lineNumber) {
		return lineMap.containsKey(lineNumber);
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		
		builder.append("Report name: " + name + "\n");
		for (Integer lineNumber : lineMap.keySet()) {
			builder.append(lineMap.get(lineNumber) + "\n");
		}
		
		return builder.toString();
	}
	
	public String printValues() {
		StringBuilder builder = new StringBuilder();
		
		builder.append("---------------------------");
		builder.append("Report name: " + name + "\n");
		for (Integer lineNumber : lineMap.keySet()) {
			builder.append(lineMap.get(lineNumber).printValues() + "\n");
		}
		
		return builder.toString();
	}

	@Override
	public Iterator<Line> iterator() {
		return lineMap.values().iterator();
	}
}
